package com.chy.blog_server.modular.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_server.modular.web.service.IBoPhotoAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：相册表 controller
 * 作者：chenhuiyan
 * 时间：2020/02/25 22:07
 */
@Controller
@RequestMapping(value = "/web/boPhotoAlbum")
public class BoPhotoAlbumController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boPhotoAlbum/";

    @Autowired
    private IBoPhotoAlbumService boPhotoAlbumService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boPhotoAlbum";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boPhotoAlbumService.list(params); //获取数据列表
            Integer count = boPhotoAlbumService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boPhotoAlbum_add";
    }

    /**
     * 保存（并删除封面图）
     * @param boPhotoAlbum
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> saveData(BoPhotoAlbum boPhotoAlbum, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            res = boPhotoAlbumService.saveData(boPhotoAlbum, request);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "添加失败");
        }
        return res;
    }


    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boPhotoAlbumService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 上传相册封面图
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadAlbumImg")
    @ResponseBody
    public Map<String, Object> uploadAlbumImg(MultipartFile file){

        //上传
        Map<String, Object> res = UploadFileUtil.fileUploadToOne(file, UploadURLUtil.PHOTO_ALBUM_IMG, false);

      return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPhotoAlbum boPhotoAlbum = boPhotoAlbumService.getById(id);
            boPhotoAlbum.setDelFlag(1);
            boPhotoAlbum.preUpdate(request);
            boPhotoAlbumService.updateById(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 修改是否加锁状态
     * @param isLock
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateIsLock")
    @ResponseBody
    public Map<String, Object> updateIsLock(@RequestParam(name = "isLock") Integer isLock,
        @RequestParam(name = "photoAlbumId") String photoAlbumId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPhotoAlbum boPhotoAlbum = boPhotoAlbumService.getById(photoAlbumId);
            boPhotoAlbum.setIsLock(isLock);
            //开启加锁时，原密码为空则设置默认密码为123456
            if(boPhotoAlbum.getIsLock() == 1){
                if(ObjectUtil.isNull(boPhotoAlbum.getPassword()) || "".equals(boPhotoAlbum.getPassword())){
                    boPhotoAlbum.setPassword("123456");
                }
            }
            boPhotoAlbum.preUpdate(request);
            boPhotoAlbumService.updateById(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 修改是否显示状态
     * @param isShow
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateIsShow")
    @ResponseBody
    public Map<String, Object> updateIsShow(@RequestParam(name = "isShow") Integer isShow,
        @RequestParam(name = "photoAlbumId") String photoAlbumId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPhotoAlbum boPhotoAlbum = boPhotoAlbumService.getById(photoAlbumId);
            boPhotoAlbum.setIsShow(isShow);
            boPhotoAlbum.preUpdate(request);
            boPhotoAlbumService.updateById(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 相册回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boPhotoAlbumRecycle";
    }

    /**
     * 查询已删除的相册列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boPhotoAlbumService.recycleList(params); //获取数据列表
            Integer count = boPhotoAlbumService.getBaseMapper().selectCount(new QueryWrapper<BoPhotoAlbum>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            boPhotoAlbumService.thoroughlyDelete(id);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复相册
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoPhotoAlbum boPhotoAlbum = boPhotoAlbumService.getById(id);
            boPhotoAlbum.setDelFlag(0);
            boPhotoAlbum.preUpdate(request);
            boPhotoAlbumService.updateById(boPhotoAlbum);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
